
from amscms.user_settings.models import UserSettings
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _

class ProfileForm(forms.ModelForm):
    """
    A form that modify a user profile
    """
    username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$',
        error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
    first_name = forms.CharField(label=_("First name"), max_length=30)
    last_name = forms.CharField(label=_("Last name"), max_length=30)
    email = forms.EmailField(label=_("E-mail"), max_length=75)
    
    def __init__(self, *args, **kwargs):
        super(ProfileForm, self).__init__(*args, **kwargs)
        
    class Meta:
        model = User
        fields = ("username", "first_name", "last_name", "email")    

class NotificationForm(forms.ModelForm):
    """
    A form that modify a user notifications
    """
    message_notify = forms.BooleanField(label=_("sends you a message"), required=False)

    def __init__(self, *args, **kwargs):
        super(NotificationForm, self).__init__(*args, **kwargs)
        
    def save(self, current_user):
        userSettings = UserSettings.objects.get(user=current_user)
        userSettings.message_notify = self.cleaned_data['message_notify']
        userSettings.save()

    class Meta:
        model = UserSettings
        fields = ("message_notify",)

class DeactivateForm(forms.ModelForm):
    """
    A form that deactivate user
    """
    is_active = forms.BooleanField(label=_("deactivate your account?"), required=False)
    
    def __init__(self, *args, **kwargs):
        super(DeactivateForm, self).__init__(*args, **kwargs)
    
    def save(self, current_user):
        user = User.objects.get(username=current_user.username)
        user.is_active = False
        user.save()
    
    class Meta:
        model = User
        fields = ("is_active",)
